# import jusmargui
from PySide2.QtWidgets import (
    QApplication,
    QWidget,
    QTableWidgetItem,
    QTableWidget,
    QHeaderView,
    QAbstractItemView,
    QVBoxLayout,
    QTabWidget,
    QGroupBox,
    QLabel,
    QLineEdit,
    QHBoxLayout,
    QGridLayout,
    QComboBox,
    QPushButton,
    QListWidget,
    QDialog,
    QButtonGroup,
    QRadioButton,
    QCheckBox,
    QListWidgetItem,
    QMessageBox,
    QSizePolicy,
    QSpacerItem,
)
from PySide2.QtCore import Qt, Signal
from PySide2.QtGui import QPixmap, QDoubleValidator, QIntValidator, QImage, QMouseEvent
import sys
import csv
import os
import json
import subprocess
import copy
from gui.controls import envs
from datetime import datetime
from .. import message_box as MBox
from ..params_table import ParamsTable
from .. import config as Config
from ..utils import Utils
from ..dlg_model_params import DlgModeResetCls
from ..custom_widget import LineEditFloat, LineEditStr, SelectBox


class TankNodeTable(ParamsTable):
    def __init__(self, rows=0, columns=2, isLibrary=False, parent=None):
        super(TankNodeTable, self).__init__(rows, columns, isLibrary, parent)
        self.contextMenu.clear()


class DlgTankSelectNode(DlgModeResetCls):
    ok_signal = Signal(int)

    def __init__(self, parent=None, params={}):
        super().__init__(parent)
        self.widgetDict = {}
        self.resize(400, 400)

        self.type = params.get("type") or "add"
        self.row = params.get("row")
        self.data = params.get("data") or []
        self.setWindowTitle("选择节点")

        layout = QVBoxLayout()
        self.table = ParamsTable(rows=0, columns=3)
        self.table.verticalHeader().setVisible(True)
        self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.table.setHorizontalHeaderLabels(
            [
                "节点号",
                "x坐标",
                "位置说明",
            ]
        )
        self.table.rowSetInt([0])
        self.table.rowSetFloat([1])
        self.table.setSelectionBehavior(QTableWidget.SelectRows)
        # 设置选择模式：只能选择单行
        self.table.setSelectionMode(QTableWidget.SingleSelection)
        layout.addWidget(self.table)

        btnLayout = QHBoxLayout()
        cancelBtn = QPushButton("取消")
        okBtn = QPushButton("确定")

        btnLayout.setAlignment(Qt.AlignCenter)
        btnLayout.addWidget(okBtn)
        btnLayout.addWidget(cancelBtn)
        layout.addLayout(btnLayout)

        okBtn.clicked.connect(self.on_okBtn_clicked)
        cancelBtn.clicked.connect(self.close)

        self.setLayout(layout)

        self.updateUI(data=self.data)

    def updateUI(self, data):
        for rowData in data:
            currentRowData = [rowData[0], rowData[1], rowData[6]]
            self.table.addRow(rowData=currentRowData)

    def on_okBtn_clicked(self):
        node = None
        selected_row = self.table.currentRow()  # 获取当前选中的行号
        if selected_row != -1:  # 如果有选中的行
            item = self.table.item(selected_row, 0)
            node = Utils.toInt(item.text())
        else:  # 如果没有选中的行
            MBox.warning("请选择节点")
            return
        self.close()
        self.ok_signal.emit(node)
